import json

from flask import Flask, request, jsonify, Response, make_response, render_template
from werkzeug.utils import secure_filename
#创建实例，传入模块名告诉根目录路径
app=Flask(__name__)

#定义路由和视图函数
@app.route('/')
def hello():
    return "Hello Flask!"

#该模式打开/say/白板
@app.route('/say')
def say():
    return "Hello JOKER!"

#该模式打开/say1/正常
@app.route('/say1/')
def say1():
    return "Hello JOKER1!"

#动态路由
@app.route('/hello/<username>')
def helloname(username):
    return f'hello {username}'


#动态路由限定参数类型，定义路由和视图函数
@app.route('/post/<int:post_id>')
def deleteitembyid(post_id):
    return f'删除{post_id}'


# get 请求
@app.route("/get")
def get():
    return f"Method is GET."

@app.route("/get_method", methods=["GET"])
def get_method():
    return f"GET method success."


# post 请求
@app.route("/post", methods=["POST"])
def post():
    return f"Method is POST."

@app.route("/post1", methods=["post","put"])
def post1():
    return f"Method is POST1."

@app.route("/postdata/", methods=["post","put"])
def post12():
    print(request.method)
    print(request.form)
    print(request.host)
    print(request.headers)
    # print(request.json)
    # print(request.json.get('height'))
    # print(request.json.get('hobby'))


    # print(request.args)
    # print(request.args.get('type'),request.args.get('attribute'))

    print(request.headers.get('Token'))
    print(request.headers.get('Cookie'))

    return request.method


@app.route("/put", methods=["PUT"])
def put():
    return f"Method is PUT."

@app.route("/delete", methods=["DELETE"])
def delete():
    return f"Method is DELETE."





# request 的常用属性/方法
# 以下是一些常见的处理请求数据的方法：
#
# 属性/方法	说明
# args	记录请求中的查询参数
# json	记录请求中的 json 数据
# files	记录请求上传的文件
# form	记录请求中的表单数据
# method	记录请求使用的 HTTP 方法
# url	记录请求的 URL 地址
# host	记录请求的域名
# headers	记录请求的头信息


@app.route('/tuple')
def res_tuple():
    #响应头{}
    return '返回元组',200,{"type":"tuple"}




@app.route('/jsondata')
def res_json():
    data={
        "name":"nim",
        "address":"beijing"
    }
    return jsonify(data)

@app.route('/uploadfile/',methods=['POST'])
def upload_file_save_file():
    print(request.files)

    files = request.files
    fileobj = files.get('filename')
    # 保存文件到目录下
    fileobj.save('./uploadfile/' + secure_filename(fileobj.filename))

    return '上传文件测试'


@app.route('/api/data')
def get_data():
    data = {
        "error": False,
        "results": [1, 2, 3, 4, 5]
    }
    return Response(
        json.dumps(data),
        mimetype='application/json',
        status=200
    )

@app.route('/extra')
def res_extra():
    res = make_response(jsonify(name="laoSAN"))
    # 设置响应头
    res.headers['name'] = "laoyan"
    # 设置cookie
    res.set_cookie("age", '18')
    res.set_cookie("gender", '男')
    return res


@app.route('/extradata')
def return_extra_data():
    data={'name':'sdv'}
    res=make_response(jsonify(data))
    #响应头
    res.headers['ohhhh']='213scew'
    #cookie信息
    res.set_cookie('adc','23')
    res.set_cookie('sex','man')

    return res

#返回html文件 创建template文件夹
@app.route('/template/')
def return_template():
    #返回html文件
    return render_template('demo.html')




@app.route("/deletemethod/deletedata", methods=["DELETE"])
def delete1():
    return f"Method is DELETE."

if __name__ == '__main__':
    app.run(debug=True)

#命令行启动
#flask --app  名 run
#临时设置
# $env:FLASK_APP="名"
#查看变量
# echo $env:FLASK_APP
#flask run






